package parser;

public class Parser {
	//attributes
	public PSetSite setSite;
	public PCreateDB createDB;
	public PCreateTable createTable;
	public PFragment fragment;
	public PInsert insert;
	public PDelete delete;
	public PImport pimport;
	public PQuery query;
	public PUse use;

	public short cmdType;
	public String command;
	//method
	public Parser(String c){
		//initial attributes
		setSite = null;
		createDB = null;
		createTable = null;
		createTable = null;
		insert = null;
		delete = null;
		query = null;
		use = null;
		cmdType = -1;
		command = new String(c);
		String cmd = new String(c);
		//remove blanks in the start
		while(cmd.startsWith(" ")) cmd = cmd.substring(1);
		//remove blanks at the end
		while(cmd.endsWith(" ") || cmd.endsWith("\n")) cmd = cmd.substring(0, cmd.length()-1);
		//set site
		if(cmd.startsWith("SET"))
		{
			setSite = new PSetSite(cmd);
			cmdType = setSite.setType;
		}
		//create database
		else if(cmd.startsWith("CREATE DATABASE"))
		{
			createDB = new PCreateDB(cmd);
			cmdType = PConstants.CREATEDB;
		}
		//create table
		else if(cmd.startsWith("CREATE TABLE"))
		{
			createTable = new PCreateTable(cmd);
			cmdType = PConstants.CREATETABLE;
		}
		else if(cmd.startsWith("FRAGMENT")){
			fragment = new PFragment(cmd);
			cmdType = PConstants.FRAGMENT;
		}
		else if(cmd.startsWith("INSERT")){
			insert = new PInsert(cmd);
			cmdType = PConstants.INSERT;
		}
		else if(cmd.startsWith("DELETE")){
			delete = new PDelete(cmd);
			cmdType = PConstants.DELETE;
		}
		else if(cmd.startsWith("IMPORT")){
			pimport = new PImport(cmd);
			cmdType = PConstants.IMPORT;
		}
		//select
		else if(cmd.startsWith("SELECT"))
		{
			query = new PQuery(cmd);
			cmdType = PConstants.SELECT;
		}
		else if(cmd.startsWith("use")){
			use = new PUse(cmd);
			cmdType = PConstants.USE;
		}
		else if(cmd.startsWith("show tables")){
			cmdType = PConstants.SHOWTABLES;
		}
		else if(cmd.startsWith("show sites")){
			cmdType = PConstants.SHOWSITES;
		}
		else{
			System.out.println("Parser:command form error!");
		}
	}//end Parser()
	public PQuery get_PQuery(){
		return query;
	}
}//end class
